<script lang="ts" setup>
import {
  getPatientConsultOrderPreAPI,
  postPatientConsultOrderAPI,
  postPatientConsultPayAPI,
} from '@/apis/consult'
import { getPatientInfoByIdAPI } from '@/apis/patient'
import { useConsultStore } from '@/stores/consult'
import type { GetPatientConsultOrderPreData } from '@/types/consult'
import type { CustomPaymentInstance } from '@/types/global-components'
import type { GetPatientMylistItem } from '@/types/sub_form'
import { onLoad } from '@dcloudio/uni-app'
import { onMounted, ref } from 'vue'

//
const store = useConsultStore()
const orderPreInfo = ref<GetPatientConsultOrderPreData>()

const getOrderPreInfo = async () => {
  const res = await getPatientConsultOrderPreAPI({
    type: store.type,
    illnessType: store.illnessType,
  })
  orderPreInfo.value = res.data
}
// 2. 获取患者信息
const orderId = ref<string>('')
const patientInfo = ref<GetPatientMylistItem>()
const getPatientInfo = async () => {
  const res = await getPatientInfoByIdAPI(store.patientId)
  // console.log(res.data)
  patientInfo.value = res.data
  orderId.value = res.data.id
  console.log('患者初步信息', res.data)
}
//提交支付
const submitPayment = async () => {
  const res = await postPatientConsultOrderAPI({
    type: Number(store.type),
    illnessType: Number(store.illnessType),
    patientId: store.patientId,
    depId: store.depId,
    illnessDesc: store.initIllnessInfo.illnessDesc,
    illnessTime: store.initIllnessInfo.illnessTime,
    consultFlag: store.initIllnessInfo.consultFlag,
    pictures: store.initIllnessInfo.pictures,
  })
  // console.log('store的患者id', store.depId)
  // console.log('------------------');

  console.log('问诊类型', Number(store.type))
  console.log('医院0/1', Number(store.illnessType))
  console.log('患者id', store.patientId)
  console.log('科室id', store.depId)
  console.log('病情描述', store.initIllnessInfo.illnessDesc)
  console.log('病情时间', store.initIllnessInfo.illnessTime)
  console.log('是否就诊过', store.initIllnessInfo.consultFlag)
  console.log('id,url',store.initIllnessInfo.pictures)

  orderId.value = res.data.id
  customPaymentRef.value?.open()

}
onLoad(() => {
  getOrderPreInfo()
  getPatientInfo()
})
//弹窗支付
const customPaymentRef = ref<CustomPaymentInstance>()
//   onMounted(() => {
//   // 检查暴露出来的方法
//   console.log('暴露出来的方法', customPaymentRef.value)
// })
/** 关闭支付弹窗 */
const handleClose = async () => {
  // 二次确认提示
  const res = await uni.showModal({
    title: '关闭支付',
    content: '取消支付将无法获得医生回复，医生接诊名额有限，是否确认关闭？',
    cancelText: '仍要关闭',
    cancelColor: '#848484',
    confirmText: '继续支付',
    confirmColor: '#16C2A3',
  })
  // 如果用户选择关闭，则关闭支付弹窗
  if (res.cancel) {
    customPaymentRef.value?.close?.()
  }
}
const handleConfirm = async (index: number) => {
  if (index === 0) {
    return uni.showToast({ title: '暂不支持微信支付', icon: 'none' })
  } else if (index == 1) {
    const res = await postPatientConsultPayAPI({
      orderId: orderId.value,
      paymentMethod: '1',
      payCallback: 'http://localhost:5173/#/subpkg_consult/pages/room/room',
    })
    //url 重定向到支付宝页面
    location.href = res.data.payUrl
  }
}

// 6.切换支付方式
const handleChange = (index: number) => {
  console.log('切换支付方式', index)
}
</script>

<template>
  <scroll-view>
    <view class="payment-page">
      <uni-section
        :title="`图文问诊${orderPreInfo?.payment}元`"
        title-font-size="32rpx"
        title-color="#121826"
        padding="30rpx"
      >
        <uni-list :border="false">
          <uni-list-item
            title="极速问诊"
            note="自动分配医生"
            thumb="/static/uploads/doctor-avatar.jpg"
            thumb-size="lg"
          />
          <uni-list-item
            title="优惠券"
            show-arrow
            :right-text="`-¥${orderPreInfo?.couponDeduction ?? '0.00'}`"
          />
          <uni-list-item title="积分抵扣">
            <template #footer>
              <view class="uni-list-text-red">-¥${{ orderPreInfo?.pointDeduction ?? '0.00' }}</view>
            </template>
          </uni-list-item>
          <uni-list-item title="实付款">
            <template #footer>
              <view class="uni-list-text-red">¥${{ orderPreInfo?.actualPayment ?? '0.00' }}</view>
            </template>
          </uni-list-item>
        </uni-list>
      </uni-section>

      <view class="dividing-line"></view>

      <uni-section title-font-size="32rpx" title-color="#121826" padding="30rpx" title="患者资料">
        <uni-list :border="false">
          <uni-list-item title="患者信息">
            <template #footer>
              <view class="uni-list-text-gray">
                {{ patientInfo?.name }} | {{ patientInfo?.genderValue }} | {{ patientInfo?.age }}岁
              </view>
            </template>
          </uni-list-item>
          <uni-list-item border title="病情描述" :note="store.initIllnessInfo.illnessDesc" />
        </uni-list>
      </uni-section>

      <!-- <view class="payment-agreement">
        <radio color="#20c6b2" value="1" />
        我已同意<text style="color: #20c6b2">支付协议</text>
      </view> -->
    </view>
    <!-- 下一步操作 -->
    <view class="next-step">
      <view class="total-amount">
        合计: <text class="number">¥{{ orderPreInfo?.actualPayment || '0.00' }}</text>
      </view>
      <button class="uni-button" @click="submitPayment">立即支付</button>
    </view>
    <custom-payment
      ref="customPaymentRef"
      :order-id="orderId"
      :amount="orderPreInfo?.actualPayment ?? 0"
      @close="handleClose"
      @change="handleChange"
      @confirm="handleConfirm"
    />
  </scroll-view>
</template>

<style lang="scss">
@import './payment.scss';
</style>
